package org.invigorare.auctions.data;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

// TODO: consider renaming this file
public class SaleDataOpenHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 2;

	public static final String SALE_TABLE_NAME 	= "auction_sales";
	public static final String SALE_TABLE_NULL_COL	= "nullIfNoData";	
	
	public static final String _ID				= "_id"; // Name must be exactly so, for Query Adapters to work
	
	// Metadata for this sale
	public static final String MARKET_NAME		= "market_name";
	public static final String EMPLOYEE_NO 		= "employee_No";
	public static final String ENTRY_ID 		= "entryId";
	public static final String TOLLRECEIPT_NO 	= "tollReceipt_No";
	public static final String SALE_TIMESTAMP	= "time_sold";
	public static final String DEPART_TIMESTAMP	= "time_departed";
	
	// Parties in this sale
	public static final String SELLER_LICENSE	= "seller_license_No";
	public static final String AGENT_LICENSE	= "agent_license_No";
	
	// Content of this sale
	public static final String SALE_PRODUCE_ID 	= "sale_produceId";
	public static final String SALE_WEIGHT		= "sale_weight";
	public static final String SALE_RATE		= "sale_rate";
	public static final String SALE_TOTAL		= "sale_total";
	
	// Fees associates with this sale
	public static final String FEE_UNLOAD 		= "fee_unload";
	public static final String FEE_CLEAN		= "fee_clean";
	public static final String FEE_WEIGH		= "fee_weigh";
	
	public static final String FEE_LOAD 		= "fee_load";
	public static final String FEE_STITCH		= "fee_stitch";
	public static final String FEE_PACK			= "fee_pack";
	
	public static final String COMMISSION_AGENT	= "commission_agent";
	public static final String COMMISSION_MANDI	= "commission_mandi";
	
	private static final String SALE_TABLE_CREATE = 
		"CREATE TABLE " + SALE_TABLE_NAME + " (" +
		_ID +	 			" INTEGER PRIMARY KEY AUTOINCREMENT, " +
		
		MARKET_NAME + 		" TEXT, " +
		EMPLOYEE_NO + 		" TEXT, " + 
		ENTRY_ID +			" TEXT, " +
		TOLLRECEIPT_NO + 	" TEXT, " +
		SALE_TIMESTAMP +	" TEXT, " +
		DEPART_TIMESTAMP +	" TEXT, " +

		SELLER_LICENSE +	" TEXT, " +
		AGENT_LICENSE + 	" TEXT, " +
		
		SALE_PRODUCE_ID +	" TEXT, " +
		SALE_WEIGHT +		" TEXT, " +
		SALE_RATE +			" TEXT, " +
		SALE_TOTAL +		" TEXT, " +
		
		FEE_UNLOAD +		" TEXT, " +
		FEE_CLEAN +			" TEXT, " +
		FEE_WEIGH +			" TEXT, " +
		FEE_LOAD +			" TEXT, " +
		FEE_STITCH +		" TEXT, " +
		FEE_PACK +			" TEXT, " +
		
		COMMISSION_AGENT +	" TEXT, " +
		COMMISSION_MANDI +	" TEXT" +
		
		");"; 

	public SaleDataOpenHelper(Context context) {
		super(context, SALE_TABLE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SALE_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	
	public static SQLiteDatabase getWritableDb(Context context) {
		SaleDataOpenHelper dbHelper = new SaleDataOpenHelper(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		return db;
	}
	
	public static SQLiteDatabase getReadableDb(Context context) {
		SaleDataOpenHelper dbHelper = new SaleDataOpenHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		return db;
	}

}
